package com.kayt.cyberBar.message.business.server.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kayt.cyberBar.common.cache.PublicCache;
import com.kayt.cyberBar.message.business.dao.IMessageDetailItemDao;
import com.kayt.cyberBar.message.business.entity.po.BMessageDetailItem;
import com.kayt.cyberBar.message.business.entity.vo.MessageDetailItemVO;
import com.kayt.cyberBar.message.business.server.IMessageDetailItemServer;

@Service("IMessageDetailItemServer")
public class MessageDetailItemServerImpl implements IMessageDetailItemServer {
	private static Logger logger = LoggerFactory.getLogger(MessageDetailItemServerImpl.class);
	
	@Autowired
	private IMessageDetailItemDao iMessageDetailItemDao;

	@Autowired
	private PublicCache publicCache;
	
	@Override
	public BMessageDetailItem get_item_by_code(String itemCode) {
		if (itemCode != null) {
			try {
				BMessageDetailItem item = this.iMessageDetailItemDao.getMessageByCode(itemCode);
				return item;
			} catch (SQLException e) {
				logger.error("get_item_by_code with a SQLException", e);
			}
		}
		return null;
	}
	
	@Override
	public MessageDetailItemVO assemble_vo(BMessageDetailItem item) {
		if (item != null) {
			MessageDetailItemVO vo = new MessageDetailItemVO();
			vo.setCode(item.getCode());
			vo.setCreateTime(item.getCode());
			vo.setDetailCode(item.getDetailCode());
			String id = item.getId() == null ? null : item.getId().toString();
			vo.setId(id);
			vo.setItemCode(item.getItemCode());
			vo.setItemType(item.getItemType());
			vo.setUpdateTime(item.getUpdateTime());
			return vo;
		}
		return null;
	}
	
	@Override
	public List<MessageDetailItemVO> assemble_vo_list (List<BMessageDetailItem> itemList) {
		List<MessageDetailItemVO> voList = new ArrayList<>();
		if (itemList != null && !itemList.isEmpty()) {
			for (BMessageDetailItem item : itemList) {
				MessageDetailItemVO vo = this.assemble_vo(item);
				voList.add(vo);
			}
		}
		return voList;
	}
}
